package com.vadmin.vadminjava.service;

import com.alibaba.fastjson.JSONObject;
import com.vadmin.vadminjava.common.ResultSetToJavaBean;
import com.vadmin.vadminjava.db.DBUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/* ***********************************************************
 *
 * 公共继承类，可能也用于注入
 * 提供一些常用的东西，省得他妈次次都注入
 *
 * **********************************************************/
@Component
public class CommonExtends {
    org.slf4j.Logger logger = LoggerFactory.getLogger(CommonExtends.class.getName());


    /**
     * 通用读取1条记录
     * @param params
     * @return
     */
    public JSONObject commonDataSingle(String sql) {
        JSONObject map = new JSONObject();
        map.put("resultFlag", false);
        DBUtil dbUtil = new DBUtil();

        //执行
        try {
            JSONObject data = ResultSetToJavaBean.copyJsonSingleQuery(dbUtil,sql);

            map.put("data", data);
            map.put("resultFlag", true);
            map.put("resultMsg", "操作成功");
        } catch (Exception e) {
            map.put("resultMsg", "操作失败");
        } finally {
            dbUtil.closeNoErr();
        }
        return map;
    }


    /**
     * 通用数据列表，分页，全部字段，不经过特殊处理
     * 这个只能用于普通的输出，如果有其它的筛选字段，用CommonExtends类
     * @param params
     * @return
     */
    public JSONObject commonDataList(JSONObject params,String table) {
        JSONObject map = new JSONObject();
        map.put("resultFlag", false);
        DBUtil dbUtil = new DBUtil();
        //sql处理
        Integer page      = params.getInteger("page");
        Integer pageSize  = params.getInteger("pageSize");
        String startTime = params.getString("startTime");
        String endTime   = params.getString("endTime");

        //参数处理
        //if(StringUtils.isEmpty(page)) page = 1;
        //if(StringUtils.isEmpty(pageSize))pageSize = 20;
        if(page==null) page = 1;
        if(pageSize==null)pageSize = 20;

        //sql处理
        String countSql = "SELECT COUNT(0) as count FROM "+table+" WHERE 1=1 ";
        String sql = "select * from "+table+" where 1=1 ";

        if(StringUtils.isNoneEmpty(startTime) && StringUtils.isNoneEmpty(startTime)){
            sql += " AND create_time >= '" + startTime + "' ";
            countSql += " AND create_time >= '" + startTime + "' ";
        }
        if(StringUtils.isNoneEmpty(endTime) && StringUtils.isNoneEmpty(endTime)) {
            sql += " AND create_time <= '" + endTime + "' ";
            countSql += " AND create_time <= '" + endTime + "' ";
        }


        sql += " order by id desc ";
        sql += " limit " + (page - 1) * pageSize + "," + pageSize;

        //执行
        try {
            ArrayList<JSONObject> objects = ResultSetToJavaBean.copyJson(dbUtil.executeQuery(sql));
            int                   count   = ResultSetToJavaBean.copyJsonSingleQuery(dbUtil,countSql).getInteger("count");


            map.put("count",count);
            map.put("data", objects);
            map.put("resultFlag", true);
            map.put("resultMsg", "操作成功");
        } catch (Exception e) {
            map.put("resultMsg", "操作失败");
        } finally {
            dbUtil.closeNoErr();
        }
        return map;
    }


    /**
     * 公共弹性列表数据读取
     * @param table
     * @param conditions
     * @param params
     * @return
     */
    public JSONObject commonDataListFlexible(String table,String conditions,JSONObject params){
        JSONObject map = new JSONObject();
        map.put("resultFlag", false);
        DBUtil dbUtil = new DBUtil();
        //sql处理
        Integer page      = params.getInteger("page");
        Integer pageSize  = params.getInteger("pageSize");

        if(page==null) page = 1;
        if(pageSize==null) pageSize = 100;

        //条件处理
        String countSql = "SELECT COUNT(0) as count FROM "+table+" WHERE 1=1 ";
        String sql = "SELECT * FROM "+table+" where 1=1 ";
        sql += conditions;//加上外部的条件，有没有都可以
        sql += " order by id desc";
        sql += " limit " + ((page - 1) * pageSize) + "," + pageSize;

        //执行
        try {
            ArrayList<JSONObject> dataRows = ResultSetToJavaBean.copyJson(dbUtil.executeQuery(sql));
            int                   count    = ResultSetToJavaBean.copyJsonSingleQuery(dbUtil,countSql).getInteger("count");
            map.put("count",count);
            map.put("data", dataRows);
            map.put("resultFlag", true);
            map.put("resultMsg", "读取成功");
        } catch (Exception e) {
            map.put("resultMsg", "读取失败");
        } finally {
            dbUtil.closeNoErr();
        }
        return map;
    }
}
